﻿/*
 * @Author: your name
 * @Date: 2022-02-08 17:16:42
 * @LastEditTime: 2022-05-02 09:34:31
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \blog_backStageSystem\blog_serve\Model\model.js
 */
const mysql = require("mysql");
const pool = mysql.createPool({
  host: "106.15.45.34",
  port: "3306",
  user: "mydb",
  password: "123456",
  database: "mydb",
});

const db = function (sql, options, callback) {
  pool.getConnection(function (err, conn) {
    if (err) {
      callback(err, null, null);
    } else {
      conn.query(sql, options, function (err, results, fields) {
        //事件驱动回调
        callback(err, results, fields);
      });
      //释放连接，需要注意的是连接释放需要在此处释放，而不是在查询回调里面释放
      conn.release();
    }
  });
};

module.exports = db;
